﻿
:- dynamic situacionActual/1,percepcion/4,accionEjecutada/2,posicion/3,
personas/3,victimario/3,esquinaVista/3,victimarioHallado/1.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                            Reglas Diagnósticas                                  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

victimario(0,0,S) :- percepcion(_,[victimario,_,_,_,_,_,_,_,_],_,_),situacionActual(S).
victimario(0,1,S) :- percepcion(_,[_,victimario,_,_,_,_,_,_,_],_,_),situacionActual(S).
victimario(0,2,S) :- percepcion(_,[_,_,victimario,_,_,_,_,_,_],_,_),situacionActual(S).
victimario(1,0,S) :- percepcion(_,[_,_,_,victimario,_,_,_,_,_],_,_),situacionActual(S).
victimario(1,1,S) :- percepcion(_,[_,_,_,_,victimario,_,_,_,_],_,_),situacionActual(S).
victimario(1,2,S) :- percepcion(_,[_,_,_,_,_,victimario,_,_,_],_,_),situacionActual(S).
victimario(2,0,S) :- percepcion(_,[_,_,_,_,_,_,victimario,_,_],_,_),situacionActual(S).
victimario(2,1,S) :- percepcion(_,[_,_,_,_,_,_,_,victimario,_],_,_),situacionActual(S).
victimario(2,2,S) :- percepcion(_,[_,_,_,_,_,_,_,_,victimario],_,_),situacionActual(S).

esquinaVista(0,0,S) :- percepcion(_,[vista,_,_,_,_,_,_,_,_],_,_),situacionActual(S).
esquinaVista(0,1,S) :- percepcion(_,[_,vista,_,_,_,_,_,_,_],_,_),situacionActual(S).
esquinaVista(0,2,S) :- percepcion(_,[_,_,vista,_,_,_,_,_,_],_,_),situacionActual(S).
esquinaVista(1,0,S) :- percepcion(_,[_,_,_,vista,_,_,_,_,_],_,_),situacionActual(S).
esquinaVista(1,1,S) :- percepcion(_,[_,_,_,_,vista,_,_,_,_],_,_),situacionActual(S).
esquinaVista(1,2,S) :- percepcion(_,[_,_,_,_,_,vista,_,_,_],_,_),situacionActual(S).
esquinaVista(2,0,S) :- percepcion(_,[_,_,_,_,_,_,vista,_,_],_,_),situacionActual(S).
esquinaVista(2,1,S) :- percepcion(_,[_,_,_,_,_,_,_,vista,_],_,_),situacionActual(S).
esquinaVista(2,2,S) :- percepcion(_,[_,_,_,_,_,_,_,_,vista],_,_),situacionActual(S).

personas(0,0,S) :- percepcion([personas,_,_,_,_,_,_,_,_],_,_,_),situacionActual(S).
personas(0,1,S) :- percepcion([_,personas,_,_,_,_,_,_,_],_,_,_),situacionActual(S).
personas(0,2,S) :- percepcion([_,_,personas,_,_,_,_,_,_],_,_,_),situacionActual(S).
personas(1,0,S) :- percepcion([_,_,_,personas,_,_,_,_,_],_,_,_),situacionActual(S).
personas(1,1,S) :- percepcion([_,_,_,_,personas,_,_,_,_],_,_,_),situacionActual(S).
personas(1,2,S) :- percepcion([_,_,_,_,_,personas,_,_,_],_,_,_),situacionActual(S).
personas(2,0,S) :- percepcion([_,_,_,_,_,_,personas,_,_],_,_,_),situacionActual(S).
personas(2,1,S) :- percepcion([_,_,_,_,_,_,_,personas,_],_,_,_),situacionActual(S).
personas(2,2,S) :- percepcion([_,_,_,_,_,_,_,_,personas],_,_,_),situacionActual(S).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                            Reglas Causales                                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

celdaConocida(X,Y,S) :- personas(X,Y,S),!.
celdaConocida(X,Y,S) :- victimario(X,Y,S),!.
celdaConocida(X,Y,S) :- esquinaVista(X,Y,S),!.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                           Axiomas de Estado Sucesor                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Cuando la accion ejecutada NO cambia la posicion actual
est(S1) :- S1 > 0, S is S1-1, accionEjecutada(sacarFoto,S),posicion(X,Y,S),asserta(posicion(X,Y,S1)),asserta(victimarioHallado(S1)).

% Cuando la accion ejecutada SI cambia la posicion actual
est(S1) :- S1 > 0,S is S1-1, posicion(X,Y,S), X > 0, accionEjecutada(irNorte,S), X1 is X-1, asserta(posicion(X1,Y,S1)).
est(S1) :- S1 > 0,S is S1-1, posicion(X,Y,S), X < 2, accionEjecutada(irSur,S), X1 is X+1, asserta(posicion(X1,Y,S1)).
est(S1) :- S1 > 0,S is S1-1, posicion(X,Y,S), Y < 2, accionEjecutada(irEste,S), Y1 is Y+1,asserta(posicion(X,Y1,S1)).
est(S1) :- S1 > 0,S is S1-1, posicion(X,Y,S), Y > 0, accionEjecutada(irOeste,S), Y1 is Y-1,asserta(posicion(X,Y1,S1)).

% Celdas Visitadas
est(S1) :- S1 > 0,S is S1-1,esquinaVisitada(X,Y,S),not(esquinaVisitada(X,Y,S1)),asserta(esquinaVisitada(X,Y,S1)).
est(S1) :- S1 > 0,S is S1-1,posicion(X,Y,S),accionEjecutada(_,S),not(esquinaVisitada(X,Y,S1)),asserta(esquinaVisitada(X,Y,S1)).
est(S1) :- S1 > 0,S is S1-1,esquinaVista(X,Y,S),not(esquinaVista(X,Y,S1)),asserta(esquinaVista(X,Y,S1)).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                            Evaluación de Acciones                               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

excelente(sacarFoto,S) :- posicion(X,Y,S),victimario(X,Y,S).

% Es muy bueno moverse hacia el victimario.
muyBueno(D,S) :- enDireccion(Xa,Ya,D,S),victimario(Xa,Ya,S).


% Es bueno moverse hacia una esquina que nos permita ver esquinas
% que nunca vimos.

bueno(D,S) :- verMasEsquinas(D,S).

% Es una acción regular, moverse a una esquina que NO nos permita ver esquinas
% nuevas, pero NO hayamos visitado
regular(D,S) :- irNoVisitada(D,S).

% Es malo, moverse a una esquina que NO nos permita ver esquinas
% nuevas, y SI hayamos visitado
malo(D,S) :- adyacente(_,_,D,S).


bestAction(noAction,S) :- goalReached(S),!.
bestAction(X,S) :- excelente(X,S),!.
bestAction(X,S) :- muyBueno(X,S),!.
bestAction(X,S) :- bueno(X,S),!.
bestAction(X,S) :- regular(X,S),!.
bestAction(X,S) :- malo(X,S),!.




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                             Extra sentences                                     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

esquinaAdyacente(X,Y,X1,Y) :- X<2,X1 is X+1.
esquinaAdyacente(X,Y,X1,Y) :- X>0,X1 is X-1.
esquinaAdyacente(X,Y,X,Y1) :- Y<2,Y1 is Y+1.
esquinaAdyacente(X,Y,X,Y1) :- Y>0,Y1 is Y-1.

esquinaEnLinea(X,Y,0,Y) :- X=:=2.
esquinaEnLinea(X,Y,2,Y) :- X=:=0.
esquinaEnLinea(X,Y,X,0) :- Y=:=2.
esquinaEnLinea(X,Y,X,2) :- Y=:=0.

adyacente(X,Y1,irOeste,S) :- posicion(X,Y,S), Y>0,  Y1 is Y-1.
adyacente(X,Y1,irEste,S) :- posicion(X,Y,S), Y<2,  Y1 is Y+1.
adyacente(X1,Y,irNorte,S) :- posicion(X,Y,S), X>0,  X1 is X-1.
adyacente(X1,Y,irSur,S) :- posicion(X,Y,S), X<2,  X1 is X+1.

enDireccion(X,Y1,irEste,S) :- posicion(X,Y,S), Y<2,  Y1 is Y+2.
enDireccion(X,Y1,irEste,S) :- posicion(X,Y,S), Y<2,  Y1 is Y+1.
enDireccion(X,Y1,irOeste,S) :- posicion(X,Y,S), Y>0,  Y1 is Y-2.
enDireccion(X,Y1,irOeste,S) :- posicion(X,Y,S), Y>0,  Y1 is Y-1.
enDireccion(X1,Y,irNorte,S) :- posicion(X,Y,S), X>0,  X1 is X-2.
enDireccion(X1,Y,irNorte,S) :- posicion(X,Y,S), X>0,  X1 is X-1.
enDireccion(X1,Y,irSur,S) :- posicion(X,Y,S), X<2,  X1 is X+2.
enDireccion(X1,Y,irSur,S) :- posicion(X,Y,S), X<2,  X1 is X+1.

verEsquinasNuevas(X,Y,S) :- esquinaAdyacente(X,Y,Xa,Ya),not(esquinaVista(Xa,Ya,S)).
verEsquinasNuevas(X,Y,S) :- esquinaEnLinea(X,Y,Xa,Ya),not(esquinaVista(Xa,Ya,S)).

verMasEsquinas(D,S) :- adyacente(X,Y,D,S),verEsquinasNuevas(X,Y,S).

irNoVisitada(D,S) :- adyacente(X,Y,D,S),not(esquinaVisitada(X,Y,S)).

discoveryDirection(D,S) :- adjacent(Xa,Ya,D,S),adjacentCell(Xa,Ya,Xaa,Yaa),not(knows(Xaa,Yaa,S)).
foodDirection(D,S) :- adjacent(Xa,Ya,D,S),adjacentCell(Xa,Ya,Xaa,Yaa),food(Xaa,Yaa,S).
enemyDirection(D,S) :- adjacent(Xa,Ya,D,S),adjacentCell(Xa,Ya,Xaa,Yaa),enemy(Xaa,Yaa,S).

%% Para saber cuándo el agente alcanzó el objetivo
todoVisto(S) :- esquinaVista(0,0,S),esquinaVista(0,1,S),esquinaVista(0,2,S),
                 esquinaVista(1,0,S),esquinaVista(1,1,S),esquinaVista(1,2,S),
                 esquinaVista(2,0,S),esquinaVista(2,1,S),esquinaVista(2,2,S).

goalReached(S) :- victimarioHallado(S),!.
goalReached(S) :- todoVisto(S),not(victimario(_,_,S),!.

and(A,B):- isTrue(A), isTrue(B).
or(A,B):- isTrue(A) ; isTrue(B).

isTrue(true).
isTrue(A):- var(A), !, false.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                    Escenarios                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



